import * as BABYLON from "babylonjs";

export class Material3D {
    private _canvas: HTMLCanvasElement;
    private _engine: BABYLON.Engine;
    private _scene: BABYLON.Scene;
    private _camera: BABYLON.FreeCamera;
    private _light: BABYLON.Light;
    constructor(canvasId: string) {
        this._canvas = document.getElementById(canvasId) as HTMLCanvasElement;
        this._engine = new BABYLON.Engine(this._canvas, true);
        this._scene = {} as BABYLON.Scene;
        this._camera = {} as BABYLON.FreeCamera;
        this._light = {} as BABYLON.Light;
    }

    createScene(): void {
        this._scene = new BABYLON.Scene(this._engine);
        this._scene.ambientColor = new BABYLON.Color3(1, 1, 1); // 设置背景颜色
        this._camera = new BABYLON.FreeCamera('mycamera', new BABYLON.Vector3(0, 5, -10), this._scene);
        this._camera.setTarget(BABYLON.Vector3.Zero());
        this._camera.attachControl(this._canvas, false);
    }

    createShape(): void {
        // 材质
        // this._light = new BABYLON.HemisphericLight('mylight', new BABYLON.Vector3(0, 1, 0), this._scene);

        // let sphere = BABYLON.MeshBuilder.CreateSphere('mysphere', {}, this._scene);
        // let myMaterial = new BABYLON.StandardMaterial('mymaterial', this._scene);
        // myMaterial.diffuseColor = new BABYLON.Color3(1, 0, 1); // 漫反射
        // myMaterial.specularColor = new BABYLON.Color3(0.5, 0.6, 0.87); //高光
        // myMaterial.emissiveColor = new BABYLON.Color3(0.1, 0.1, 0.1); // 自发光
        // myMaterial.ambientColor = new BABYLON.Color3(0.23, 0.98, 0.13); // 环境
        // sphere.material = myMaterial;

        // 材质 Test2
        let light2 = new BABYLON.HemisphericLight('light2', new BABYLON.Vector3(-1, 1, 0), this._scene);
        light2.diffuse = new BABYLON.Color3(1, 0, 0); //普通光照为红色
        light2.specular = new BABYLON.Color3(0, 1, 0); //高光为绿色
        light2.groundColor = new BABYLON.Color3(0, 1, 0); //地面光照为绿色

        let material1 = new BABYLON.StandardMaterial('material1', this._scene); //材质1
        material1.ambientColor = new BABYLON.Color3(1, 0, 0);

        let material2 = new BABYLON.StandardMaterial('material2', this._scene); //材质2
        material2.ambientColor = new BABYLON.Color3(0, 1, 0);

        let sphere1 = BABYLON.MeshBuilder.CreateSphere('sphere1', {}, this._scene);
        sphere1.position.x = -1.5;

        let sphere2 = BABYLON.MeshBuilder.CreateSphere('sphere2', {}, this._scene);
        sphere2.position.x = 0;
        sphere2.material = material1; // 添加红色环境色材质的球

        let sphere3 = BABYLON.MeshBuilder.CreateSphere('sphere3', {}, this._scene);
        sphere3.position.x = 1.5;
        sphere3.material = material2; // 添加了绿色环境色材质的球

        // 材质 - 透明
        // this._light = new BABYLON.HemisphericLight('mylight', new BABYLON.Vector3(0, 1, 0), this._scene);

        // var material = new BABYLON.StandardMaterial('opacity', this._scene);
        // let sphere = BABYLON.MeshBuilder.CreateSphere('mysphere', {}, this._scene);
        // sphere.scaling = new BABYLON.Vector3(4, 4, 4);
        // material.alpha = 0.1;
        // material.wireframe = true;
        // material.backFaceCulling = false;
        // sphere.material = material;
    }

    doRender(): void {
        // Run the render loop.
        this._engine.runRenderLoop(() => {
            this._scene.render();
        });

        // The canvas/window resize event handler.
        window.addEventListener('resize', () => {
            this._engine.resize();
        });
    }
}